// @algorithm @lc id=100316 lang=cpp
// @title di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof

#include "algm/algm.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
    // @test("leetcode")="l"
// @test("leetcode")="l"
public:
    char firstUniqChar(string s) {
        if (s.empty()) {
            return ' ';
        }
        vector<bool> appeared(26, false);
        vector<int> position(26, -1);
        int n = s.length();
        for (int i = 0; i < n; ++i) {
            int index = s[i] - 'a';
            if (!appeared[index]) {
                appeared[index] = true;
                position[index] = i;
            }
            else {
                position[index] = -1;
            }
        }
        int ans = -1;
        for (int i = 0; i < 26; ++i) {
            if (appeared[i] && position[i] != -1) {
                if (ans == -1) {
                    ans = position[i];
                }
                else {
                    ans = min(position[i], ans);
                }
            }
        }
        return ans == -1 ? ' ' : s[ans];
    }
};